﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Delonix_Regia_Hotel_Management_System.Class;

namespace Delonix_Regia_Hotel_Management_System
{
    public partial class CuustomerInformation : Form
    {
        SqlConnection conn = new SqlConnection();

        public CuustomerInformation()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (LogInClass.position == "Administrator")
            {
                Main__Management_and_Administrator_ mainA = new Main__Management_and_Administrator_();
                this.Hide();
                mainA.Show();
            }
            else
            {
                Main__Receptionist_ mainR = new Main__Receptionist_();
                this.Hide();
                mainR.Show();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 F = new Form1();
            this.Hide();
            F.Show(); 
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            List<RoomBooking> roomBookingList = DBManager.getAllRoomBooking();
            listBox1.Items.Clear();
            for (int i = 0; i < roomBookingList.Count; i++)
            {
                listBox1.Items.Add(roomBookingList[i].bookingId);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                string bookingId = Convert.ToString(listBox1.SelectedItem);
                List<RoomBooking> RoomBookingList = DBManager.getAllRoomBookingByBookingId(bookingId);
                for (int i = 0; i < RoomBookingList.Count; i++)
                {
                    if (listBox1.SelectedItem.Equals(RoomBookingList[i].bookingId))
                    {
                        dataGridView1.DataSource = RoomBookingList;
                        dataGridView1.Columns["nric"].Visible = false;

                        string nric = dataGridView1.Rows[0].Cells[1].Value.ToString();
                        List<Guest> GuestList = DBManager.getAllGuestbyNric(nric);
                        for (int j = 0; j < GuestList.Count; j++)
                        {
                            dataGridView2.DataSource = GuestList;
                        }
                    }
                }
            }
        }

        private void btnRetrieve_Click(object sender, EventArgs e)
        {
            List<RoomBooking> roomBooking = DBManager.getAllRoomBooking();
            dataGridView1.DataSource = roomBooking;
            dataGridView1.Columns["nric"].Visible = false;
        }

        private void btnRetrieveGuest_Click(object sender, EventArgs e)
        {
            List<Guest> guest = DBManager.getAllGuest();
            dataGridView2.DataSource = guest;
        }
        
        private void Bind()
        {
            conn.ConnectionString = "data source=localhost; initial catalog=DelonixRegia; user id=user; password=Password_2013";
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * Staff", conn);
            //DataTable dt = new DataTable();
            //da.Fill(dt);
            dataGridView1.DataSource = da;
            conn.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            conn.ConnectionString = "data source=localhost; initial catalog=DelonixRegia; user id=user; password=Password_2013";
            conn.Open();

            for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            {

                SqlCommand cmd = new SqlCommand("Update RoomBooking set bookingId=@bookingId, numOfAdult=@numOfAdult,numOfChildren=@numOfChildren, roomType=@roomType, additionalRemarks=@additionalRemarks, checkInDate=@checkInDate, checkOutDate=@checkOutDate, paymentMethod=@paymentMethod, roomAllocated=@roomAllocated Where(bookingId=@bookingId)", conn);
                cmd.Parameters.AddWithValue("@bookingId", dataGridView1.Rows[i].Cells[0].Value.ToString());
                cmd.Parameters.AddWithValue("@numOfAdult", dataGridView1.Rows[i].Cells[2].Value.ToString());
                cmd.Parameters.AddWithValue("@numOfChildren", dataGridView1.Rows[i].Cells[3].Value.ToString());
                cmd.Parameters.AddWithValue("@roomType", dataGridView1.Rows[i].Cells[4].Value.ToString());
                cmd.Parameters.AddWithValue("@checkInDate", dataGridView1.Rows[i].Cells[5].Value.ToString());
                cmd.Parameters.AddWithValue("@checkOutDate", dataGridView1.Rows[i].Cells[6].Value.ToString());
                cmd.Parameters.AddWithValue("@additionalRemarks", dataGridView1.Rows[i].Cells[7].Value.ToString());
                cmd.Parameters.AddWithValue("@paymentMethod", dataGridView1.Rows[i].Cells[8].Value.ToString());
                cmd.Parameters.AddWithValue("@roomAllocated", dataGridView1.Rows[i].Cells[9].Value.ToString());
                cmd.ExecuteNonQuery();

            }
            conn.Close();
            MessageBox.Show("updated");
            Bind();
        }

        private void btnUpdateGuest_Click(object sender, EventArgs e)
        {
            conn.ConnectionString = "data source=localhost; initial catalog=DelonixRegia; user id=user; password=Password_2013";
            conn.Open();

            for (int i = 0; i <= dataGridView2.Rows.Count - 1; i++)
            {

                SqlCommand cmd = new SqlCommand("Update Guest set nric=@nric, homeMailingAddress=@homeMailingAddress, email=@email, phoneNum=@phoneNum Where(nric=@nric)", conn);
                cmd.Parameters.AddWithValue("@nric", dataGridView2.Rows[i].Cells[0].Value.ToString());
                cmd.Parameters.AddWithValue("@homeMailingAddress", dataGridView2.Rows[i].Cells[4].Value.ToString());
                cmd.Parameters.AddWithValue("@email", dataGridView2.Rows[i].Cells[5].Value.ToString());
                cmd.Parameters.AddWithValue("@phoneNum", dataGridView2.Rows[i].Cells[3].Value.ToString());
                cmd.ExecuteNonQuery();

            }
            conn.Close();
            MessageBox.Show("updated");
            Bind();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you really want to delete selected record(s)?",
                "Delete warning", MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false)
                == DialogResult.Yes)
            {
                conn.ConnectionString = "data source=localhost; initial catalog=DelonixRegia; user id=user; password=Password_2013";
                SqlCommand delcmd = new SqlCommand();
                string bookingId = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                if (dataGridView1.Rows.Count > 0)
                {
                    delcmd.CommandText = "DELETE FROM RoomBooking WHERE bookingId=@bookingId";
                    conn.Open();
                    delcmd.Connection = conn;
                    delcmd.Parameters.AddWithValue("@bookingId", bookingId);
                    delcmd.ExecuteNonQuery();
                    conn.Close();
                    MessageBox.Show("Row Deleted");
                }
                Bind();
            }
        }

        private void btnDeleteGuest_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you really want to delete selected record(s)?",
                "Delete warning", MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false)
                == DialogResult.Yes)
            {
                conn.ConnectionString = "data source=localhost; initial catalog=DelonixRegia; user id=user; password=Password_2013";
                SqlCommand delcmd = new SqlCommand();
                string nric = dataGridView2.SelectedRows[0].Cells[0].Value.ToString();
                if (dataGridView2.Rows.Count > 0)
                {
                    delcmd.CommandText = "DELETE FROM Guest WHERE nric=@nric";
                    conn.Open();
                    delcmd.Connection = conn;
                    delcmd.Parameters.AddWithValue("@nric", nric);
                    delcmd.ExecuteNonQuery();
                    conn.Close();
                    MessageBox.Show("Row Deleted");
                }
                Bind();
            }
        }
    }
}
